<?php
// +----------------------------------------------------------------------
// | 中联致远服务端框架
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------

namespace app\common\model;
use app\common\model\AssistantUserRel as AssistantUserRelModel;
use think\facade\Log;

class BookApply extends Common
{
    /**
     * @param $post
     *
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function tableData($post)
    {
        if(isset($post['limit'])){
            $limit = $post['limit'];
        }else{
            $limit = config('paginate.list_rows');
        }

        $tableWhere = $this->tableWhere($post);
        $list = $this->distinct(true)
            ->field('ba.*, vs.season_name as season, vd.bmmc as dep, vsp.zymc as specialty, vc.plan_class_name as class, vdr.name as dir, u.mobile, u.truename as teacher, ifnull(plannum.plan_student, 0) - ifnull(batchnum.num, 0) - ifnull(prenum.num, 0) as sysl')
            ->alias('ba')
            ->leftjoin('zl_view_get_seasons vs', 'vs.season_id = ba.season')
            ->leftjoin('zl_view_get_depts vd', 'vd.bmdm = ba.dep_id')
            ->leftjoin('zl_view_get_specicalties vsp', 'vsp.zydm = ba.specialty_id and vsp.bmdm = ba.dep_id ')
            ->leftjoin('zl_view_get_classes vc', 'vc.plan_class_id = ba.class_id and vc.bmdm = ba.dep_id and vc.zydm = ba.specialty_id ')
            ->leftjoin('zl_view_get_dirs vdr', 'vdr.id = ba.dir')
            ->leftjoin('zl_user u', 'u.id = ba.teacherid')
            ->leftjoin('zl_book_plan plannum', 'plannum.plan_code = ba.class_id')
            ->leftjoin('zl_view_get_batchdraw_num batchnum', 'batchnum.class_id = ba.class_id')
            ->leftjoin('zl_view_get_predraw_num prenum', 'prenum.class_id = ba.class_id')
            ->where($tableWhere['where'])
            ->order($tableWhere['order'])
            ->paginate($limit);
        // $list = $this->field($tableWhere['field'])->where($tableWhere['where'])->order($tableWhere['order'])->paginate($limit);
        $data = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型

        $data1 = $this->distinct(true)
            ->field('ba.*, vs.season_name as season, vd.bmmc as dep, vsp.zymc as specialty, vc.plan_class_name as class, vdr.name as dir, u.mobile, u.truename as teacher, ifnull(plannum.plan_student, 0) - ifnull(batchnum.num, 0) - ifnull(prenum.num, 0) as sysl')
            ->alias('ba')
            ->leftjoin('zl_view_get_seasons vs', 'vs.season_id = ba.season')
            ->leftjoin('zl_view_get_depts vd', 'vd.bmdm = ba.dep_id')
            ->leftjoin('zl_view_get_specicalties vsp', 'vsp.zydm = ba.specialty_id and vsp.bmdm = ba.dep_id ')
            ->leftjoin('zl_view_get_classes vc', 'vc.plan_class_id = ba.class_id and vc.bmdm = ba.dep_id and vc.zydm = ba.specialty_id ')
            ->leftjoin('zl_view_get_dirs vdr', 'vdr.id = ba.dir')
            ->leftjoin('zl_user u', 'u.id = ba.teacherid')
            ->leftjoin('zl_book_plan plannum', 'plannum.plan_code = ba.class_id')
            ->leftjoin('zl_view_get_batchdraw_num batchnum', 'batchnum.class_id = ba.class_id')
            ->leftjoin('zl_view_get_predraw_num prenum', 'prenum.class_id = ba.class_id')
            ->where($tableWhere['where'])
            ->order($tableWhere['order'])
            ->select();
        
        $re['code'] = 0;
        $re['msg'] = '';
        $re['count'] = count($data1);
        $re['data'] = $data;
        return $re;
    }

    /**
     *  修改状态
     * @param $data
     * @return array
     */
    public function changeStatus($data)
    {
        $result = [
            'status' => false,
            'msg'    => '发放失败',
            'data'   => [],
        ];

        $ids_arr = explode(",", $data['ids']);

        $dataList = [];
        foreach($ids_arr as $val) {
            $dataList[] = array('id'=>$val, "is_ff"=>"1");

            // 发放后增加签收记录并更新签收状态
            $model = new Common();
            $model->query("call bookDraw('".$val."');");
        }

        # 发放成功
        if($this->isUpdate(true)->saveAll($dataList))
        {
            $result['status'] = true;
            $result['msg']    = '发放成功';
        }
        \think\facade\Request::token('__ZlServer_Token__', 'sha1');
        return $result;
    }

    //where搜索条件
    protected function tableWhere($post)
    {
        $where = [];
        if(isset($post['id']) && $post['id'] != ""){
            $where[] = ['ba.id', 'eq', $post['id']];
        }
        if(isset($post['year']) && $post['year'] != ""){
            $where[] = ['year', 'eq', $post['year']];
        }
        if(isset($post['season']) && $post['season'] != ""){
            $where[] = ['season', 'eq', $post['season']];
        }
        if(isset($post['dep_id']) && $post['dep_id'] != ""){
            $where[] = ['dep_id', 'eq', $post['dep_id']];
        }
        if(isset($post['specialty_id']) && $post['specialty_id'] != ""){
            $where[] = ['specialty_id', 'eq', $post['specialty_id']];
        }
        if(isset($post['class_id']) && $post['class_id'] != ""){
            $where[] = ['ba.class_id', 'eq', $post['class_id']];
        }
        if(isset($post['dir']) && $post['dir'] != ""){
            $where[] = ['dir', 'eq', $post['dir']];
        }
        if(isset($post['is_ff']) && $post['is_ff'] != ""){
            $where[] = ['is_ff', 'eq', $post['is_ff']];
        }
        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = ['id ASC'];
        return $result;
    }


    /**
     * @param $list
     *
     * @return mixed
     */
    protected function tableFormat($list)
    {
        foreach($list as &$val){
            $val['is_ff'] = $val['is_ff'] == 0?'未发放':'已发放';  
        }
        return $list;
    }

    /**
     *  获取学生状态
     * @param $data
     * @return array
     */
    public function studentIsReady($year, $season, $xh)
    {
        $where = []; 
        $where[] = ['apply.year', 'eq', $year]; 
        $where[] = ['apply.season', 'eq', $season]; 
        $where[] = ['detail.xh', 'eq', $xh]; 

        $studentInfo = $this->field('apply.is_ff')
            ->alias('apply')
            ->leftjoin('zl_book_apply_detail detail', 'apply.id = detail.applyid')
            ->where($where)
            ->find();

        if($studentInfo && $studentInfo['is_ff'] == 1){
            return true;
        } else {
            return false;
        }
    }
    
    /**
     *  获取学生状态
     * @param $data
     * @return array
     */
    public function getIsqs($year, $season, $xh)
    {
        $where = []; 
        $where[] = ['apply.year', 'eq', $year]; 
        $where[] = ['apply.season', 'eq', $season]; 
        $where[] = ['detail.xh', 'eq', $xh]; 

        $studentInfo = $this->field('apply.teacherid, apply.class_id, detail.is_qs')
            ->alias('apply')
            ->leftjoin('zl_book_apply_detail detail', 'apply.id = detail.applyid')
            ->where($where)
            ->find();

        return $studentInfo;
    }
}
